package com.show.mybatis.factory;

import com.show.mybatis.config.Configuration;
import com.show.mybatis.config.Mapper;
import com.show.mybatis.utils.Executor;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;

public class MapperProxyFactory implements InvocationHandler {
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        String className = method.getDeclaringClass().getName();

        String methodName = method.getName();

        String key = className + "." + methodName;

        Configuration config = new Configuration();

        Map<String, Mapper> mappers = config.getMappers();

        Mapper mapper = mappers.get(key);

        if ("findAll".equals(methodName)){
            return Executor.selectList(mapper,config.createDataSource().getConnection());
        }
        return null;
    }
}
